<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //递归深拷贝

      //1.实现一个对象的深拷贝
      /*   var obj1 = {
        name: "张三",
        age: 20,
      };

      //   console.log(obj1);

      var obj2 = {};

      for (var key in obj1) {
        obj2[key] = obj1[key];
      }
      console.log(obj2);
      //修改
      obj2.name = "hello";
      console.log(obj1); //不会影响obj1 */

      //创建深拷贝函数
      /*     function deepCopy(obj1) {
        var obj2 = {};
        for (var key in obj1) {
          obj2[key] = obj1[key];
        }
        return obj2;
      }
      var obj2 = deepCopy(obj1);
      console.log(obj2); */

      //实现一个数组的深拷贝
      //这个加了判断能区分是数组还是对象

      /* function deepCopy(obj1) {
        //判断obj1的类型 是数组还是对象 如果是数组  创建一个新的数组 如果是对象那么就创建一个新对象
        var obj2 = {};
        var obj2 =
          Object.prototype.toString.call(obj1) === "[object Array]" ? [] : {};
        for (var key in obj1) {
          obj2[key] = obj1[key];
        }
        return obj2;
      }

      //
      var arr = ["张三", "李四", "王五"];
      console.log(arr);
      var obj = { name: 123 };
      var arr2 = deepCopy(obj);
      console.log(arr2); */
      /* console.log(typeof []);
      console.log(typeof {}); 都是object*/
      //通过constructor  通过Object.prototype.toString.call([])
      // console.log([].constructor === Array);
      // console.log({}.constructor === Object);
      // console.log(Object.prototype.toString.call([]));
      // console.log(Object.prototype.toString.call({}));
      // console.log(Object.prototype.toString.call(true));
      // console.log(Object.prototype.toString.call(123));
      // console.log(Object.prototype.toString.call("back"));
      // console.log(Object.prototype.toString.call(/\d+/));
      // console.log(Object.prototype.toString.call(null));
      // console.log(typeof null);

      // console.log(Object.prototype.toString.call(undefined));
      // console.log(Object.prototype.toString.call(function () {}));
      // console.log(typeof function () {});

      //3.递归深拷贝
      //缺点就是不能深层次拷贝内容

      function deepCopy(obj1) {
        var obj2 =
          Object.prototype.toString.call(obj1) === "[object Array]" ? [] : {};
        for (var key in obj1) {
          if (obj1.hasOwnProperty(key)) {
            // 判断是否是对象自身属性；
            if (typeof obj1[key] === "object") {
              // console.log("键值是数组或者是对象",obj1[key]);
              obj2[key] = deepCopy(obj1[key]);
            } else {
              // console.log("简单数据类型",obj1[key]);
              obj2[key] = obj1[key];
            }
          }
        }
        return obj2;
      }

      var obj1 = {
        name: "张三",
        age: 20,
        arr: [1, 2, 3],
        myhobby: {
          one: "篮球",
          two: "足球",
        },
      };
      console.log("obj1", obj1);
      var obj2 = deepCopy(obj1);
      console.log("obj2", obj2);
      // obj2.name = "hello";
      obj2.myhobby.one = "修改了";
      console.log("obj1", obj1);
    </script>
  </body>
</html>
